﻿CREATE PROCEDURE [dbo].[utl_LogMsg]
@Message VARCHAR (MAX)=NULL, @ErrorCode INT=NULL, @RowsAffected INT=NULL, @ProcessName [sysname]=NULL, @StepName [sysname]=NULL, @Instance UNIQUEIDENTIFIER=NULL
AS
BEGIN
	/*
	include these statements in the stored procedure that will call this stored procedure:
	
	-- cut here --
	-- start of utl_LogMsg setup --
	DECLARE @Message        VARCHAR(MAX);     SET @Message = ISNULL(@Message, '{message is null}')
	DECLARE @ErrorCode      INT;
	DECLARE @RowsAffected   INT;
	DECLARE @ProcessName    sysname;          SET @ProcessName = '<process name>'
	DECLARE @ProcedureName  sysname;          SET @ProcedureName = ISNULL(@ProcedureName, OBJECT_NAME(@@PROC_ID))
	DECLARE @Instance       UNIQUEIDENTIFIER; SET @Instance = ISNULL(@Instance, NEWID())
	-- end of utl_LogMsg setup --
	-- cut here --
	*/
	
	SET @Message = ISNULL(@Message, '{message is null}')
	
	INSERT INTO MessageLog
	  (
	    [Message],
	    RowsAffected,
	    ErrorCode,
	    ProcessName,
	    ProcedureName,
	    CreateDt,
	    Instance
	  )
	VALUES
	  (
	    @Message,
	    @RowsAffected,
	    @ErrorCode,
	    @ProcessName,
	    @StepName,
	    GETDATE(),
	    @Instance
	  )
END

